package patrol.mobile.com.mobilepatrol.activity.toll;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.JobEvaluationInformationBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

public class EvaluationDetail2Activity extends BaseActivity {
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.exListView)
    ExpandableListView exListView;
    private JobEvaluationInformationBean bean;
    private List<String> parentList;
    private List<String> childList;
    private Map<String, List<JobEvaluationInformationBean.OrgPostItemListBean>> map = new HashMap<>();

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EvaluationDetail2Activity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("考核明细");

        bean = (JobEvaluationInformationBean) getIntent().getSerializableExtra("list");
        tv1.setText("被考核人：" + bean.getCheckName());
        tv2.setText("考核人：" + bean.getAssessor());
        tv3.setText("总扣分：" + bean.getDeductScore() + "分");
        parentList = new ArrayList<>();
        childList = new ArrayList<>();
        for (JobEvaluationInformationBean.OrgPostItemListBean orgPostItemListBean : bean.getOrgPostItemList()) {
            parentList.add(orgPostItemListBean.getProSmall());
            childList.add(orgPostItemListBean.getProContent());
        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            parentList = parentList.stream().distinct().collect(Collectors.toList());
        }
        exListView.setAdapter(new Adapter());
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_evaluation_detail2;
    }

    @Override
    public void setListener() {
        exListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Intent intent = new Intent(EvaluationDetail2Activity.this, EvaluationContentActivity.class);
                intent.putExtra("list", bean);
                intent.putExtra("position", i);
                startActivity(intent);
                return true;
            }
        });
    }

    //自定义适配器
    class Adapter extends BaseExpandableListAdapter {
        //获取子元素对象
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        //获取子元素Id
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //加载子元素并显示
        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            View view = null;
            ChildHolder childholder = null;
            if (convertView != null) {
                view = convertView;
                childholder = (ChildHolder) view.getTag();
            } else {
                view = View.inflate(EvaluationDetail2Activity.this, R.layout.item_evaluation_detail_child, null);
                childholder = new ChildHolder();
                childholder.tv_num_child = (TextView) view.findViewById(R.id.tv_num_child);
                childholder.tv_title_child = (TextView) view.findViewById(R.id.tv_title_child);
                childholder.tv_kou = (TextView) view.findViewById(R.id.tv_kou);
                childholder.tv_yu = (TextView) view.findViewById(R.id.tv_yu);
                view.setTag(childholder);
            }
//            childholder.tv_num_child.setText((groupPosition + 1) + "." + (childPosition + 1));
            childholder.tv_title_child.setText(bean.getOrgPostItemList().get(groupPosition).getProContent());
            childholder.tv_kou.setText("扣" + bean.getOrgPostItemList().get(groupPosition).getAccount() + "分");
            childholder.tv_yu.setText("余" + bean.getOrgPostItemList().get(groupPosition).getTotalScore() + "分");
            return view;
        }

        //获取子元素数目
        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        //获取组元素对象
        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        //获取组元素数目
        @Override
        public int getGroupCount() {
            return parentList.size();
        }

        //获取组元素Id
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //加载并显示组元素
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            View view = null;
            GroupHolder groupholder = null;
            if (convertView != null) {
                view = convertView;
                groupholder = (GroupHolder) view.getTag();
            } else {
                view = View.inflate(EvaluationDetail2Activity.this, R.layout.item_evaluation_detail, null);
                groupholder = new GroupHolder();
                groupholder.tv_num = (TextView) view.findViewById(R.id.tv_num);
                groupholder.tv_title = (TextView) view.findViewById(R.id.tv_title);
                groupholder.iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
                groupholder.re_spand = (RelativeLayout) view.findViewById(R.id.re_spand);
                view.setTag(groupholder);
            }
            groupholder.tv_num.setText(groupPosition + 1 + "");
            groupholder.tv_title.setText(parentList.get(groupPosition));
            groupholder.re_spand.setVisibility(View.GONE);
            if (isExpanded) {
                groupholder.iv_arrow.setImageResource(R.mipmap.arrow_d);
            } else {
                groupholder.iv_arrow.setImageResource(R.mipmap.arrow_u);
            }
            return view;
        }

        @Override
        public boolean hasStableIds() {

            return true;
        }


        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    static class GroupHolder {
        TextView tv_num;
        TextView tv_title;
        ImageView iv_arrow;
        RelativeLayout re_spand;
    }

    static class ChildHolder {
        TextView tv_num_child;
        TextView tv_title_child;
        TextView tv_kou;
        TextView tv_yu;
    }
}
